package org.tinygroup.earthworm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.tinygroup.commons.tools.StringUtil;

import com.alibaba.fastjson.JSONObject;

public class EarthWormJsonSupport implements LogSupport {
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

	public String getLogMessage(String stage) {

		RpcContext rpcContext = RpcContext.get();
		JSONObject json = new JSONObject();
		json.put("module_type", stage);
		json.put("support_type", "EarthWormJsonSupport");
		json.put("trace_id", rpcContext.traceId);
		json.put("span_id", rpcContext.spanId);
		json.put("timestamp", dateFormat.format(new Date()));
		json.put("name", rpcContext.tag);
		json.put("ip", rpcContext.target);
		if (StringUtil.isBlank(rpcContext.errorMsg)) {
			json.put("responseStatus", 0);
		} else {
			json.put("responseStatus", 1);
		}
		json.put("spanType", 0);
		JSONObject myPackage = new JSONObject();
		Map<String, String> customInofos = rpcContext.customInofos;
		for (String key : customInofos.keySet()) {
			myPackage.put(key, customInofos.get(key));
		}
		json.put("myPackage", myPackage.toJSONString());
		return json.toJSONString();
	}

}
